import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token:localStorage.getItem('token') || ''
    token:'',
    userInfo:{}
  },
  mutations: {
    updateToken(state, token) {
           state.token = token
          // localStorage.setItem('token',token)
    },
    updateUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions:{
   async getUserInfo(context) {
      const {data:res} = await axios.get('/my/userinfo')
      if(res.code !== 0) return
      context.commit('updateUserInfo', res.data)
      // console.log(res.data);
    }
  }
 
})
